local skel = fk.CreateSkill({
  name = "kpaI__yizhenghe",
})

local KleeU = require "packages/klee_xy_I/utility/k_utility"

Fk:loadTranslationTable{
  ["kpaI__yizhenghe"] = "以正和",
  [":kpaI__yizhenghe"] = "当你不因此技能而弃牌/摸牌时，你可以取消之；若如此做，则你下次不能如此做，然后你下次摸牌/弃牌改为重铸等量的牌。",

  ["@kpaI__yizhenghe1"] = "以正和-摸牌",
  ["@kpaI__yizhenghe2"] = "以正和-弃牌",
  ["#kpaI__yizhenghe1"] = "以正和：你可以取消摸牌，然后你下次弃牌改为重铸等量的牌",
  ["#kpaI__yizhenghe2"] = "以正和：你可以取消弃牌，然后你下次摸牌改为重铸等量的牌",

  ["$kpaI__yizhenghe1"] = "以利动之，以卒待之。",
  ["$kpaI__yizhenghe2"] = "拜将非荣，用兵唯实。",
}

skel:addEffect(fk.BeforeDrawCard, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    return data.skillName ~= skel.name and player:hasSkill(skel.name) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("@kpaI__yizhenghe2") > 0 then
      player.room:notifySkillInvoked(player, skel.name)
      player:broadcastSkillInvoke(skel.name)
      local cards = room:askToChooseCards(player,{
        min = data.num,
        max = data.num,
        target = player,
        flag = "he",
        skill_name = skel.name,
      })
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
      end
      room:setPlayerMark(player,"@kpaI__yizhenghe2",0)
      data.num = 0
      return
    end
    if player:getMark("@kpaI__yizhenghe1") > 0 then return end
    if KleeU.askYesOrNo(player, "#kpaI__yizhenghe1", skel.name) then
      player.room:notifySkillInvoked(player, skel.name)
      player:broadcastSkillInvoke(skel.name)
      room:setPlayerMark(player,"@kpaI__yizhenghe1",1)
      data.num = 0
    end
  end
})

skel:addEffect(fk.BeforeCardsMove, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard and move.skillName ~= skel.name then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard and move.skillName ~= skel.name then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(cards,info.cardId)
        end
      end
    end
    if player:getMark("@kpaI__yizhenghe1") > 0 then
      player.room:notifySkillInvoked(player, skel.name)
      player:broadcastSkillInvoke(skel.name)
      room:recastCard(cards, player, skel.name)
      room:setPlayerMark(player,"@kpaI__yizhenghe1",0)
      --
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard and move.skillName ~= skel.name then
          move.moveInfo = {}
        end
      end
      --
      return
    end
    if player:getMark("@kpaI__yizhenghe2") > 0 then return end
    if KleeU.askYesOrNo(player, "#kpaI__yizhenghe2", skel.name) then
      player.room:notifySkillInvoked(player, skel.name)
      player:broadcastSkillInvoke(skel.name)
      room:setPlayerMark(player,"@kpaI__yizhenghe2",1)
      --
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard and move.skillName ~= skel.name then
          move.moveInfo = {}
        end
      end
      --
    end
  end
})

return skel